package code.example.javassist.reflect;

import java.lang.reflect.Method;

public class MethodReflector {
    Method method = null;
    Class [] parameterType = null;
    public MethodReflector(Method method){
        this.method = method;
        this.parameterType = method.getParameterTypes();
    }
    public boolean isMatch(String methodName,Object []parameter){
        String name = method.getName();
        if(methodName.equals(name)){
            if(parameter == null || parameter.length==0){
                if(parameterType == null || parameterType.length ==0){
                    return true;
                }else{
                    return false;
                }
            }else  if(parameterType == null || parameterType.length ==0){
                return false;
            }else if(parameter.length != parameterType.length){
                return false;
            }
            for(int i=0;i<parameterType.length;i++){
                if(parameter [i] != null){
                    if(this.parameterType[i] !=parameter[i].getClass()){
                        return false;
                    }
                }
            }

        }
        return true;
    }

    private Class [] getParameterTypes (Object [] parameter){
        if(parameter == null || parameter.length==0){
            return new Class [0];
        }else{
            Class [] classTypes = new Class [parameter.length];
            for(int i=0;i<parameter.length;i++){
                if(parameter[i]== null){
                    classTypes[i] = null;
                }else{
                    classTypes[i] = parameter[i].getClass();
                }
            }
            return classTypes;
        }
    }
}
